import { ElLoading } from 'element-plus';
import i18n from '@/language/i18n';

let loadingCount: number = 0;//当前触发loading请求数量

let loading = null;//ElLoading实例

const options = {//loading实例配置
    lock: true,
    text: i18n.global.t('public.loading'),
    // spinner: 'el-icon-loading',//自定义图标
    background: 'rgba(0, 0, 0, 0.7)'
};

/**
 * 创建loading
 */
function createLoading(): void {
    if (loading) return;
    loading = ElLoading.service(options);
}

/**
 * 显示loading动画
 */
export function showLoading(): void {
    loadingCount++;
    createLoading();
}

/**
 * 关闭loading动画
 */
export function closeLoading(): void {
    if (!loading) return;
    loadingCount--;
    if (loadingCount === 0) {
        setTimeout(() => {//延迟0.5s关闭loading 防止闪动
            loading.close();
            loading = null;
        }, 500);
    };
}
